using System;
using System.Collections;
using System.Security.Principal;
using System.Web;

using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.UserManagement;

namespace BEGDC.CC.BL.Workflow
{
	/// <summary>
	/// Summary description for CODMIdentity.
	/// </summary>
	public class CCIdentity : IIdentity
	{
		private bool is_auth;

		/// <summary>
		/// construction function
		/// </summary>
		/// <param name="identity">identity object</param>
		public CCIdentity(IIdentity identity)
		{
			this.id = identity;
			this.LoadEmployeeInformation();
		}

		/// <summary>
		/// load current employee information
		/// </summary>
		private void LoadEmployeeInformation()
		{			
			try
			{
//				if (HttpContext.Current.Session["CurrentUserInfor"] != null) 
//				{
//					this.user_info = (Employee)HttpContext.Current.Session["CurrentUserInfor"];
//					this.backup = (IList)HttpContext.Current.Session["CurrentUserBackup"];
//				}
//				else
//				{
				IUser userservice = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.UserService] as IUser;
				this.user_info = userservice.GetUser(this.id.Name);
				this.backup = userservice.GetBackups(this.user_info.Id);
//					HttpContext.Current.Session["CurrentUserInfor"] = this.user_info;
//					HttpContext.Current.Session["CurrentUserBackup"] = this.backup;
//				}
				is_auth = this.user_info.IsEnabled;
			}
			catch(Exception e)
			{
				System.Diagnostics.Debug.WriteLine(e.Message);
				is_auth = false;
                throw;
			}
		}
		private IIdentity id;

		private IList backup;
		/// <summary>
		/// User's backuper list
		/// </summary>
		public IList Backupers
		{
			get
			{
//				if (this.user_info == null)
//				{
//					this.LoadEmployeeInformation();
//				}
				return this.backup;
			}
		}
		
		private Employee user_info;
		/// <summary>
		/// User infomation object
		/// </summary>
		public Employee UserInfo
		{
			get
			{
//				if (this.user_info == null)
//				{
//					this.LoadEmployeeInformation();
//				}
				//===================== Added by Joe Feng 3/16/2007 ====================
				//To fix "my participation" when login as a backup
				//
				//return this.user_info;
				//

				if( this.backuper == null )
					return this.user_info;
				else
				{
					if( this.Name == this.backuper.NameEN )
						return this.backuper;
					else
						return this.user_info;
				}

				//==========================> End <============================

			}
		}

		/// <summary>
		/// User infomation object
		/// </summary>
		public Employee ActualUserInfo
		{
			get
			{
				return this.user_info;
			}
		}

		/// <summary>
		/// Has backuper
		/// </summary>
		public bool IsBackup
		{
			get
			{
//				if ((HttpContext.Current.Session == null) || (HttpContext.Current.Session["BAKIdentity"] == null))
//					return false;
//				return true;
				if (backuper == null)
					return false;
				return true;
			}
		}

		private Employee backuper = null;
		/// <summary>
		/// User's backuper
		/// </summary>
		public Employee Backuper
		{
			get
			{
//				if ((HttpContext.Current.Session == null) || (HttpContext.Current.Session["BAKIdentity"] == null))
//					return null;
//				return (Employee)HttpContext.Current.Session["BAKIdentity"];
				return backuper;
			}
			set
			{
				backuper = value;
			}
		}

		/// <summary>
		/// reload employee information
		/// </summary>
		public void Reload()
		{
			this.LoadEmployeeInformation();
		}

		#region IIdentity Members

		/// <summary>
		/// Is user authenticated
		/// </summary>
		public bool IsAuthenticated
		{
			get
			{
				return this.is_auth;
			}
		}

		/// <summary>
		/// user's name
		/// </summary>
		public string Name
		{
			get
			{
				if (this.backuper != null)
					return this.backuper.NameEN;
				return this.user_info.NameEN;
//				if ((HttpContext.Current.Session == null) || (HttpContext.Current.Session["BAKIdentity"] == null))
//                    return this.id.Name;
//				Employee backup = (Employee)HttpContext.Current.Session["BAKIdentity"];
//				return backup.ADName;
			}
		}

		/// <summary>
		/// Authentication type
		/// </summary>
		public string AuthenticationType
		{
			get
			{
				return "CC Windows Authentication";
			}
		}

		#endregion
	}
}
